<?php
define('LOCAL_DEV', false);
define('DB_DRIVER', 'mysql');
define('DB_HOSTNAME', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'example');
define('DB_PREFIX', 'oc_');

require_once(Yii::getPathOfAlias('webroot')."/class.db.mysql.php");
//Created by: Loipn
//Debug a variable
// function debug($data){
// 	echo '<pre>';
// 	print_r($data);
// 	echo '</pre>';
// }

/**
 * Clone directory
 */
/*function clone_directory($src, $dst)
{
	$dir = opendir($src);
	@mkdir($dst . '/' . $file);
	while (false !== ( $file = readdir($dir)) ) {
		if (( $file != '.' ) && ( $file != '..' )) {
			if (is_dir($src . '/' . $file)) {
				clone_directory($src . '/' . $file, $dst . '/' . $file);
			} else {
				copy($src . '/' . $file, $dst . '/' . $file);
			}
		}
	}
	closedir($dir);
	return true;
}*/

//Created by: Loipn
class installShop{
	var $parentDirectory;
	var $sourceDirectory;
	var $destinationDirectory;
	var $destinationName;
	var $shopFolderName;
	var $db;
	var $dirDefault;
	var $shopDirectory;
	
	function __construct($shopFolderName=''){
		$this->sourceDirectory = dirname(__FILE__) .DIRECTORY_SEPARATOR.'web'.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR;
		$this->parentDirectory = dirname(__FILE__) .DIRECTORY_SEPARATOR.'web'.DIRECTORY_SEPARATOR;
		$this->shopFolderName = $shopFolderName;
		$this->destinationDirectory = $this->parentDirectory.$shopFolderName.DIRECTORY_SEPARATOR;
		
		$this->shopDirectory = dirname(__FILE__) .DIRECTORY_SEPARATOR.'shop'.DIRECTORY_SEPARATOR;
		$this->dirDefault = dirname(__FILE__) .DIRECTORY_SEPARATOR;
	}

	//Create a folder to contain the source
	public function createFolder($name){
		if(!is_dir($this->parentDirectory.$name)){
			$this->shopFolderName = $name;
			mkdir($this->parentDirectory.$name, 0755);
			$this->destinationDirectory = $this->parentDirectory.$name.DIRECTORY_SEPARATOR;
			return true;
		}
		return false;
	}

	//Copy source code to a folder
	public function copyShop($name = 'shop'){
		$this->destinationName = $name;

		/*$dir = opendir($this->sourceDirectory . $name);
		while (false !== ( $file = readdir($dir)) ) {
			if (( $file != '.' ) && ( $file != '..' )) {
				$src = $this->sourceDirectory . $name;
				$dst = $this->destinationDirectory;
				if (is_dir($src . '/' . $file)) {
					clone_directory($src . '/' . $file, $dst . '/' . $file);
				} else {
					copy($src . '/' . $file, $dst . '/' . $file);
				}
			}
		}
		closedir($dir);*/

		if (DIRECTORY_SEPARATOR == '/') {// Linux
			$source = escapeshellarg($this->shopDirectory . $name) . '/*';
			$dest = $this->parentDirectory;
			$command = system("yes | cp -rf ".$source. " ".escapeshellarg($dest));
		}else if (DIRECTORY_SEPARATOR == '\\') {// Windows
			$source = $this->shopDirectory . $name . '\\*';
			$dest = $this->parentDirectory;
			system("xcopy ".escapeshellarg($source) . " ".escapeshellarg($dest) . " /E /Y /Q");
		}

		return false;
	}

	//Extract source code
	public function extractShop($name = 'shop.zip'){
		$zip = new ZipArchive;
		if ($zip->open($this->shopDirectory.$name) === TRUE) {
			$zip->extractTo($this->parentDirectory);
			$zip->close();
			//$this->deleteFile($name);
			return true;
		}
		return false;
	}

	//Delete the source code after extract
	public function deleteFile($name = 'shop.zip'){
		if(file_exists($this->destinationDirectory.$name)){
			@unlink($this->destinationDirectory.$name);
		}
		
	}

	public function deleteDbFolder($name = 'db'){
		$this->deleteFolder($this->destinationDirectory.$name);
	}

	//Delete a folder
	public function deleteFolder($dir){
		if (DIRECTORY_SEPARATOR == '/') {// Linux
			system("rm -rf ".escapeshellarg($dir));
		}else if (DIRECTORY_SEPARATOR == '\\') {// Windows
			system("rmdir ".escapeshellarg($dir) . " /s /q");
		}		
	}
	//create database and import all table, data to the new database
	//this function ned the class.db.php class
	public function processDatabase($dbName = 'computer', $theme, $admin = 'admin', $pass = '123456'){
		if (LOCAL_DEV != true)
			$data=array('db_host'=>DB_HOSTNAME,'db_user'=>'root','db_password'=>'','db_prefix'=>'oc_','db_database'=>$dbName);
		else
			$data=array('db_host'=>DB_HOSTNAME,'db_user'=>'root','db_password'=>'','db_prefix'=>'oc_','db_database'=>$dbName);

		$salt = substr(md5(uniqid(rand(), true)), 0, 9);
		$password = sha1($salt . sha1($salt . sha1($pass)));

		$this->db = new dbmysql("mysql:host=".$data['db_host'].";port=3306;",$data['db_user'],$data['db_password']);
		$this->db->run('CREATE DATABASE '.$dbName .' CHARACTER SET utf8 COLLATE utf8_unicode_ci');
		$this->db->run('USE '.$dbName);
		
		$file = $this->shopDirectory . 'db' . DIRECTORY_SEPARATOR . $theme . '.sql';
		
		if (!file_exists($file)) { 
			exit('Could not load sql file: ' . $file); 
		}
		$lines = file($file);
		if ($lines) {
			
			$sql = '';
			try{
				foreach($lines as $line) {
					if ($line && (substr($line, 0, 2) != '--') && (substr($line, 0, 1) != '#')) {
						$sql .= $line;
						if (preg_match('/;\s*$/', $line)) {
							$sql = str_replace("DROP TABLE IF EXISTS `oc_", "DROP TABLE IF EXISTS `" . $data['db_prefix'], $sql);
							$sql = str_replace("CREATE TABLE `oc_", "CREATE TABLE `" . $data['db_prefix'], $sql);
							$sql = str_replace("INSERT INTO `oc_", "INSERT INTO `" . $data['db_prefix'], $sql);
							$sql = str_replace("'admin', '3663c1951e495f68eff8257260dfec95ef626858', 'f67272e61'", "'{$admin}', '{$password}', '{$salt}'", $sql);
							$this->db->run($sql);
							$sql = '';
						}
					}
				}

				$this->db->run("SET CHARACTER SET utf8");
		
				$this->db->run("SET @@session.sql_mode = 'MYSQL40'");
				
				$this->db->run("UPDATE `" . $data['db_prefix'] . "product` SET `viewed` = '0'");
				return true;
			} catch (Exception $e) {
				return false;
			}
		}
		return false;
	}
	//Create config file
	public function createConfigFile($dbName = 'computer', $theme = ''){
		$hostName = "danatrade.vn"; //$_SERVER['HTTP_HOST']
		if(file_exists($this->destinationDirectory.'config.php')){
			@unlink($this->destinationDirectory.'config.php');
		}
		$output  = '<?php' . "\n";
		$output .= '// HTTP' . "\n";
		$output .= 'define(\'HTTP_SERVER\', \'http://' . $this->shopFolderName. '.' . $hostName.'/'. '\');' . "\n\n";
		
		$output .= '// HTTPS' . "\n";
		$output .= 'define(\'HTTPS_SERVER\', \'http://' . $this->shopFolderName. '.' . $hostName.'/' . '\');' . "\n\n";
								
		$output .= '// DIR' . "\n";
		$output .= 'define(\'DIR_APPLICATION\', dirname(__FILE__) . \'/catalog/\');' . "\n";
		$output .= 'define(\'DIR_SYSTEM\', dirname(__FILE__) . \'/../source/_common/system/\');' . "\n";
		$output .= 'define(\'DIR_DATABASE\', dirname(__FILE__) . \'/../source/_common/system/database/\');' . "\n";
		$output .= 'define(\'DIR_LANGUAGE\', dirname(__FILE__) . \'/catalog/language/\');' . "\n";
		$output .= 'define(\'DIR_TEMPLATE\', dirname(__FILE__) . \'/catalog/view/theme/\');' . "\n";
		$output .= 'define(\'DIR_CONFIG\', dirname(__FILE__) . \'/../source/_common/system/config/\');' . "\n";
		$output .= 'define(\'DIR_IMAGE\', dirname(__FILE__) . \'/image/\');' . "\n";
		$output .= 'define(\'DIR_CACHE\', dirname(__FILE__) . \'/system/cache/\');' . "\n";
		$output .= 'define(\'DIR_DOWNLOAD\', dirname(__FILE__) . \'/download/\');' . "\n";
		$output .= 'define(\'DIR_LOGS\', dirname(__FILE__) . \'/system/logs/\');' . "\n\n";

		$output .= '// Setting' . "\n";
		$output .= 'define(\'INIT_THEME\', \''.$theme.'\');' . "\n\n";

		$output .= '// DB' . "\n";
		$output .= 'define(\'DB_DRIVER\', \'' . DB_DRIVER . '\');' . "\n";
		$output .= 'define(\'DB_HOSTNAME\', \'' . DB_HOSTNAME . '\');' . "\n";
		$output .= 'define(\'DB_USERNAME\', \'' . 'root' . '\');' . "\n";
		if (LOCAL_DEV != true)
			$output .= 'define(\'DB_PASSWORD\', \'' . '' . '\');' . "\n";
		else
			$output .= 'define(\'DB_PASSWORD\', \'' . '' . '\');' . "\n";
		$output .= 'define(\'DB_DATABASE\', \'' . $dbName . '\');' . "\n";
		$output .= 'define(\'DB_PREFIX\', \'' . 'oc_' . '\');' . "\n";
		$output .= '?>';				
	
		$file = fopen($this->destinationDirectory . 'config.php', 'w');
	
		fwrite($file, $output);

		fclose($file);
 
		$output  = '<?php' . "\n";
		$output .= '// HTTP' . "\n";
		$output .= 'define(\'HTTP_SERVER\', \'http://' . $this->shopFolderName. '.' . $hostName.'/' . 'admin/\');' . "\n";
		$output .= 'define(\'HTTP_CATALOG\', \'http://' . $this->shopFolderName. '.' . $hostName.'/' . '\');' . "\n\n";
		$output .= 'define(\'HTTP_TRADE_SERVER\', \'http://' . $hostName.'/' . '\');' . "\n\n";
		
		$output .= '// HTTPS' . "\n";
		$output .= 'define(\'HTTPS_SERVER\', \'http://' . $this->shopFolderName. '.' . $hostName.'/' . 'admin/\');' . "\n";
		$output .= 'define(\'HTTPS_CATALOG\', \'http://' . $this->shopFolderName. '.' . $hostName.'/' . '\');' . "\n\n";
		
		$output .= '// DIR' . "\n";
		$output .= 'define(\'DIR_APPLICATION\', dirname(__FILE__) . \'/admin/\');' . "\n";
		$output .= 'define(\'DIR_SYSTEM\', dirname(__FILE__) . \'/../source/_common/system/\');' . "\n";
		$output .= 'define(\'DIR_DATABASE\', dirname(__FILE__) . \'/../source/_common/system/database/\');' . "\n";
		$output .= 'define(\'DIR_LANGUAGE\', dirname(__FILE__) . \'/admin/language/\');' . "\n";
		$output .= 'define(\'DIR_TEMPLATE\', dirname(__FILE__) . \'/admin/view/template/\');' . "\n";
		$output .= 'define(\'DIR_CONFIG\', dirname(__FILE__) . \'/../source/_common/system/config/\');' . "\n";
		$output .= 'define(\'DIR_IMAGE\', dirname(__FILE__) . \'/image/\');' . "\n";
		$output .= 'define(\'DIR_CACHE\', dirname(__FILE__) . \'/system/cache/\');' . "\n";
		$output .= 'define(\'DIR_DOWNLOAD\', dirname(__FILE__) . \'/download/\');' . "\n";
		$output .= 'define(\'DIR_LOGS\', dirname(__FILE__) . \'/system/logs/\');' . "\n";
		$output .= 'define(\'DIR_CATALOG\', dirname(__FILE__) . \'/catalog/\');' . "\n\n";

		$output .= '// DB' . "\n";
		$output .= 'define(\'DB_DRIVER\', \'' . DB_DRIVER . '\');' . "\n";
		$output .= 'define(\'DB_HOSTNAME\', \'' . DB_HOSTNAME . '\');' . "\n";
		$output .= 'define(\'DB_USERNAME\', \'' . 'root' . '\');' . "\n";
		if (LOCAL_DEV != true)
			$output .= 'define(\'DB_PASSWORD\', \'' . '' . '\');' . "\n";
		else
			$output .= 'define(\'DB_PASSWORD\', \'' . '' . '\');' . "\n";
		$output .= 'define(\'DB_DATABASE\', \'' . $dbName . '\');' . "\n";
		$output .= 'define(\'DB_PREFIX\', \'' . 'oc_' . '\');' . "\n";
		$output .= '?>';	

		$file = fopen($this->destinationDirectory . 'admin_config.php', 'w');
	
		fwrite($file, $output);

		fclose($file);


 
		$output  = '<?php' . "\n";
		$output .= '// HTTP' . "\n";
		$output .= 'define(\'HTTP_SERVER\', \'http://' . $this->shopFolderName. '.' . $hostName.'/' . 'admin/\');' . "\n";
		$output .= 'define(\'HTTP_CATALOG\', \'http://' . $this->shopFolderName. '.' . $hostName.'/' . '\');' . "\n\n";
		$output .= 'define(\'HTTP_TRADE_SERVER\', \'http://' . $hostName.'/' . '\');' . "\n\n";
		
		$output .= '// HTTPS' . "\n";
		$output .= 'define(\'HTTPS_SERVER\', \'http://' . $this->shopFolderName. '.' . $hostName.'/' . 'admin/\');' . "\n";
		$output .= 'define(\'HTTPS_CATALOG\', \'http://' . $this->shopFolderName. '.' . $hostName.'/' . '\');' . "\n\n";
		
		$output .= '// DIR' . "\n";
		$output .= 'define(\'DIR_APPLICATION\', dirname(__FILE__) . \'/../admin/\');' . "\n";
		$output .= 'define(\'DIR_SYSTEM\', dirname(__FILE__) . \'/../../source/_common/system/\');' . "\n";
		$output .= 'define(\'DIR_DATABASE\', dirname(__FILE__) . \'/../../source/_common/system/database/\');' . "\n";
		$output .= 'define(\'DIR_LANGUAGE\', dirname(__FILE__) . \'/../admin/language/\');' . "\n";
		$output .= 'define(\'DIR_TEMPLATE\', dirname(__FILE__) . \'/../admin/view/template/\');' . "\n";
		$output .= 'define(\'DIR_CONFIG\', dirname(__FILE__) . \'/../../source/_common/system/config/\');' . "\n";
		$output .= 'define(\'DIR_IMAGE\', dirname(__FILE__) . \'/../image/\');' . "\n";
		$output .= 'define(\'DIR_CACHE\', dirname(__FILE__) . \'/../system/cache/\');' . "\n";
		$output .= 'define(\'DIR_DOWNLOAD\', dirname(__FILE__) . \'/../download/\');' . "\n";
		$output .= 'define(\'DIR_LOGS\', dirname(__FILE__) . \'/../system/logs/\');' . "\n";
		$output .= 'define(\'DIR_CATALOG\', dirname(__FILE__) . \'/../catalog/\');' . "\n\n";

		$output .= '// DB' . "\n";
		$output .= 'define(\'DB_DRIVER\', \'' . DB_DRIVER . '\');' . "\n";
		$output .= 'define(\'DB_HOSTNAME\', \'' . DB_HOSTNAME . '\');' . "\n";
		$output .= 'define(\'DB_USERNAME\', \'' . 'root' . '\');' . "\n";
		if (LOCAL_DEV != true)
			$output .= 'define(\'DB_PASSWORD\', \'' . '' . '\');' . "\n";
		else
			$output .= 'define(\'DB_PASSWORD\', \'' . '' . '\');' . "\n";
		$output .= 'define(\'DB_DATABASE\', \'' . $dbName . '\');' . "\n";
		$output .= 'define(\'DB_PREFIX\', \'' . 'oc_' . '\');' . "\n";
		$output .= '?>';

		$file = fopen($this->destinationDirectory . 'admin/config.php', 'w');
	
		fwrite($file, $output);

		fclose($file);
		return true;
	}
	
	public function createConfigFileTwo($dbName = 'computer', $theme = ''){
		$hostName = "danatrade.vn"; //$_SERVER['HTTP_HOST']
		if(file_exists($this->destinationDirectory.'config.php')){
			return false;
		}
		$output  = '<?php' . "\n";
		$output .= '// HTTP' . "\n";
		$output .= 'define(\'HTTP_SERVER\', \'http://' . $this->shopFolderName. '.' . $hostName.'/'. '\');' . "\n\n";
		
		$output .= '// HTTPS' . "\n";
		$output .= 'define(\'HTTPS_SERVER\', \'http://' . $this->shopFolderName. '.' . $hostName.'/' . '\');' . "\n\n";
								
		$output .= '// DIR' . "\n";
		$output .= 'define(\'DIR_APPLICATION\', dirname(__FILE__) . \'/catalog/\');' . "\n";
		$output .= 'define(\'DIR_SYSTEM\', dirname(__FILE__) . \'/../source/_common/system/\');' . "\n";
		$output .= 'define(\'DIR_DATABASE\', dirname(__FILE__) . \'/../source/_common/system/database/\');' . "\n";
		$output .= 'define(\'DIR_LANGUAGE\', dirname(__FILE__) . \'/catalog/language/\');' . "\n";
		$output .= 'define(\'DIR_TEMPLATE\', dirname(__FILE__) . \'/catalog/view/theme/\');' . "\n";
		$output .= 'define(\'DIR_CONFIG\', dirname(__FILE__) . \'/../source/_common/system/config/\');' . "\n";
		$output .= 'define(\'DIR_IMAGE\', dirname(__FILE__) . \'/image/\');' . "\n";
		$output .= 'define(\'DIR_CACHE\', dirname(__FILE__) . \'/system/cache/\');' . "\n";
		$output .= 'define(\'DIR_DOWNLOAD\', dirname(__FILE__) . \'/download/\');' . "\n";
		$output .= 'define(\'DIR_LOGS\', dirname(__FILE__) . \'/system/logs/\');' . "\n\n";

		$output .= '// Setting' . "\n";
		$output .= 'define(\'INIT_THEME\', \''.$theme.'\');' . "\n\n";

		$output .= '// DB' . "\n";
		$output .= 'define(\'DB_DRIVER\', \'' . DB_DRIVER . '\');' . "\n";
		$output .= 'define(\'DB_HOSTNAME\', \'' . DB_HOSTNAME . '\');' . "\n";
		$output .= 'define(\'DB_USERNAME\', \'' . 'root' . '\');' . "\n";
		if (LOCAL_DEV != true)
			$output .= 'define(\'DB_PASSWORD\', \'' . '' . '\');' . "\n";
		else
			$output .= 'define(\'DB_PASSWORD\', \'' . '' . '\');' . "\n";
		$output .= 'define(\'DB_DATABASE\', \'' . $dbName . '\');' . "\n";
		$output .= 'define(\'DB_PREFIX\', \'' . 'oc_' . '\');' . "\n";
		$output .= '?>';				
	
		$file = fopen($this->destinationDirectory . 'config.php', 'w');
	
		fwrite($file, $output);

		fclose($file);
 
		$output  = '<?php' . "\n";
		$output .= '// HTTP' . "\n";
		$output .= 'define(\'HTTP_SERVER\', \'http://' . $this->shopFolderName. '.' . $hostName.'/' . 'admin/\');' . "\n";
		$output .= 'define(\'HTTP_CATALOG\', \'http://' . $this->shopFolderName. '.' . $hostName.'/' . '\');' . "\n\n";
		$output .= 'define(\'HTTP_TRADE_SERVER\', \'http://' . $hostName.'/' . '\');' . "\n\n";
		
		$output .= '// HTTPS' . "\n";
		$output .= 'define(\'HTTPS_SERVER\', \'http://' . $this->shopFolderName. '.' . $hostName.'/' . 'admin/\');' . "\n";
		$output .= 'define(\'HTTPS_CATALOG\', \'http://' . $this->shopFolderName. '.' . $hostName.'/' . '\');' . "\n\n";
		
		$output .= '// DIR' . "\n";
		$output .= 'define(\'DIR_APPLICATION\', dirname(__FILE__) . \'/admin/\');' . "\n";
		$output .= 'define(\'DIR_SYSTEM\', dirname(__FILE__) . \'/../source/_common/system/\');' . "\n";
		$output .= 'define(\'DIR_DATABASE\', dirname(__FILE__) . \'/../source/_common/system/database/\');' . "\n";
		$output .= 'define(\'DIR_LANGUAGE\', dirname(__FILE__) . \'/admin/language/\');' . "\n";
		$output .= 'define(\'DIR_TEMPLATE\', dirname(__FILE__) . \'/admin/view/template/\');' . "\n";
		$output .= 'define(\'DIR_CONFIG\', dirname(__FILE__) . \'/../source/_common/system/config/\');' . "\n";
		$output .= 'define(\'DIR_IMAGE\', dirname(__FILE__) . \'/image/\');' . "\n";
		$output .= 'define(\'DIR_CACHE\', dirname(__FILE__) . \'/system/cache/\');' . "\n";
		$output .= 'define(\'DIR_DOWNLOAD\', dirname(__FILE__) . \'/download/\');' . "\n";
		$output .= 'define(\'DIR_LOGS\', dirname(__FILE__) . \'/system/logs/\');' . "\n";
		$output .= 'define(\'DIR_CATALOG\', dirname(__FILE__) . \'/catalog/\');' . "\n\n";

		$output .= '// DB' . "\n";
		$output .= 'define(\'DB_DRIVER\', \'' . DB_DRIVER . '\');' . "\n";
		$output .= 'define(\'DB_HOSTNAME\', \'' . DB_HOSTNAME . '\');' . "\n";
		$output .= 'define(\'DB_USERNAME\', \'' . 'root' . '\');' . "\n";
		if (LOCAL_DEV != true)
			$output .= 'define(\'DB_PASSWORD\', \'' . '' . '\');' . "\n";
		else
			$output .= 'define(\'DB_PASSWORD\', \'' . '' . '\');' . "\n";
		$output .= 'define(\'DB_DATABASE\', \'' . $dbName . '\');' . "\n";
		$output .= 'define(\'DB_PREFIX\', \'' . 'oc_' . '\');' . "\n";
		$output .= '?>';	

		$file = fopen($this->destinationDirectory . 'admin_config.php', 'w');
	
		fwrite($file, $output);

		fclose($file);


 
		$output  = '<?php' . "\n";
		$output .= '// HTTP' . "\n";
		$output .= 'define(\'HTTP_SERVER\', \'http://' . $this->shopFolderName. '.' . $hostName.'/' . 'admin/\');' . "\n";
		$output .= 'define(\'HTTP_CATALOG\', \'http://' . $this->shopFolderName. '.' . $hostName.'/' . '\');' . "\n\n";
		$output .= 'define(\'HTTP_TRADE_SERVER\', \'http://' . $hostName.'/' . '\');' . "\n\n";
		
		$output .= '// HTTPS' . "\n";
		$output .= 'define(\'HTTPS_SERVER\', \'http://' . $this->shopFolderName. '.' . $hostName.'/' . 'admin/\');' . "\n";
		$output .= 'define(\'HTTPS_CATALOG\', \'http://' . $this->shopFolderName. '.' . $hostName.'/' . '\');' . "\n\n";
		
		$output .= '// DIR' . "\n";
		$output .= 'define(\'DIR_APPLICATION\', dirname(__FILE__) . \'/../admin/\');' . "\n";
		$output .= 'define(\'DIR_SYSTEM\', dirname(__FILE__) . \'/../../source/_common/system/\');' . "\n";
		$output .= 'define(\'DIR_DATABASE\', dirname(__FILE__) . \'/../../source/_common/system/database/\');' . "\n";
		$output .= 'define(\'DIR_LANGUAGE\', dirname(__FILE__) . \'/../admin/language/\');' . "\n";
		$output .= 'define(\'DIR_TEMPLATE\', dirname(__FILE__) . \'/../admin/view/template/\');' . "\n";
		$output .= 'define(\'DIR_CONFIG\', dirname(__FILE__) . \'/../../source/_common/system/config/\');' . "\n";
		$output .= 'define(\'DIR_IMAGE\', dirname(__FILE__) . \'/../image/\');' . "\n";
		$output .= 'define(\'DIR_CACHE\', dirname(__FILE__) . \'/../system/cache/\');' . "\n";
		$output .= 'define(\'DIR_DOWNLOAD\', dirname(__FILE__) . \'/../download/\');' . "\n";
		$output .= 'define(\'DIR_LOGS\', dirname(__FILE__) . \'/../system/logs/\');' . "\n";
		$output .= 'define(\'DIR_CATALOG\', dirname(__FILE__) . \'/../catalog/\');' . "\n\n";

		$output .= '// DB' . "\n";
		$output .= 'define(\'DB_DRIVER\', \'' . DB_DRIVER . '\');' . "\n";
		$output .= 'define(\'DB_HOSTNAME\', \'' . DB_HOSTNAME . '\');' . "\n";
		$output .= 'define(\'DB_USERNAME\', \'' . 'root' . '\');' . "\n";
		if (LOCAL_DEV != true)
			$output .= 'define(\'DB_PASSWORD\', \'' . '' . '\');' . "\n";
		else
			$output .= 'define(\'DB_PASSWORD\', \'' . '' . '\');' . "\n";
		$output .= 'define(\'DB_DATABASE\', \'' . $dbName . '\');' . "\n";
		$output .= 'define(\'DB_PREFIX\', \'' . 'oc_' . '\');' . "\n";
		$output .= '?>';

		$file = fopen($this->destinationDirectory . 'admin/config.php', 'w');
	
		fwrite($file, $output);

		fclose($file);
		return true;
	}
	//install the whole thing
	public function processInstall($name='testing'){
		//Create folder
		if($this->createFolder($name)){
			//Copy source code
			if($this->copyShop()){
				//Extract zip file
				if($this->extractShop()){
					//Create and extract database
					if($this->processDatabase(array('db_host'=>'localhost','db_user'=>'root','db_password'=>'','db_prefix'=>'oc_','db_database'=>'testing_opencart'))){
						if($this->createConfigFile()){
							return true;
						}
					}
				}
			}
		}
		return false;
	}
	// function delete database
	public function deleteDb($name){
		$link = mysql_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD);
		
		$sql = 'DROP DATABASE '.$name;
		if (mysql_query($sql, $link)) {
			return true;
		} else {
			return 'Error dropping database: ' . mysql_error() . "\n";
		}
	}
}


/*if(isset($_GET)){
	if(!empty($_GET['shop_name']) && !empty($_GET['category']) && !empty($_GET['step'])){
		switch ($_GET['step']) {
			case 'create_folder':
				if(!empty($_GET['shop_name'])){
					if($_GET['shop_name']!='source'){
						$install = new installShop();
						$install->createFolder(strtolower($_GET['shop_name']));
						echo 'ok';
					}
				}
				break;
			case 'copy_shop':
				if(!empty($_GET['shop_name'])){
					$install = new installShop($_GET['shop_name']);
					$install->copyShop($_GET['category']);
					echo 'ok';
				}
				break;
			case 'extract_shop':
				if(!empty($_GET['shop_name'])){
					$install = new installShop($_GET['shop_name']);
					$install->extractShop($_GET['category']);
					echo 'ok';
				}
				break;
			case 'process_database':
				if(!empty($_GET['shop_name'])){
					$install = new installShop($_GET['shop_name']);
					$install->processDatabase(strtolower($_GET['shop_name']),$_GET['category']);
					echo 'ok';
				}
				break;
			case 'create_config_file':
				if(!empty($_GET['shop_name'])){
					$install = new installShop(strtolower($_GET['shop_name']));
					$install->createConfigFile(strtolower($_GET['shop_name']));
					echo 'ok';
				}
				break;
			default:
				# code...
				break;
		}
	}
}*/
/*if($install->processInstall()){
	echo 'done';
}else{
	echo 'failed';
}*/